package com.example.gitzuoye3;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

/**
 * 首页Activity，包含底部导航栏和Fragment容器
 * 负责管理各个功能模块的切换
 */
public class shouye extends AppCompatActivity {
    private SharedPreferences sp;          // 用于存储用户偏好设置
    private TextView showhello;            // 顶部欢迎文本
    private ImageButton btnHome, btnRandom, btnPaihang, btnProfile; // 底部导航按钮
    private Handler handler = new Handler(); // 用于延迟操作

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shouye);

        // 1. 初始化视图组件
        showhello = findViewById(R.id.ding);
        btnHome = findViewById(R.id.btn_home);
        btnRandom = findViewById(R.id.btn_dianying);
        btnPaihang = findViewById(R.id.btn_favorites); // 热点排行按钮
        btnProfile = findViewById(R.id.btn_profile); // 个人中心按钮

        // 2. 获取用户资料
        sp = getSharedPreferences("user_profile", MODE_PRIVATE);

        // 3. 设置欢迎消息
        String username = sp.getString("username", "");
        if (showhello != null) {
            showhello.setText("欢迎你！" + (!username.isEmpty() ? username : "用户"));

            // 3秒后隐藏欢迎消息
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    showhello.setVisibility(View.GONE);
                }
            }, 3000);
        } else {
            Toast.makeText(this, "登录成功！", Toast.LENGTH_LONG).show();
        }

        // 4. 设置底部导航按钮点击事件
        btnHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openxiangxi(); // 打开电影列表
            }
        });

        btnRandom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openSuijiFragment(); // 打开随机推荐
            }
        });

        btnPaihang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openPaihangFragment(); // 打开排行榜
            }
        });

        btnProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openXinxiFragment(); // 打开个人中心
            }
        });

        // 5. 默认打开电影列表
        openxiangxi();
    }

    /**
     * 打开电影列表Fragment
     */
    private void openxiangxi() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        xiangxi foodListFragment = new xiangxi();
        fragmentTransaction.replace(R.id.dianying_list, foodListFragment);
        fragmentTransaction.commit();
    }

    /**
     * 打开随机推荐Fragment
     */
    private void openSuijiFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        Fragment suijiFragment = new tuijian();
        fragmentTransaction.replace(R.id.dianying_list, suijiFragment);
        fragmentTransaction.commit();
    }

    /**
     * 打开排行榜Fragment
     */
    private void openPaihangFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        Fragment paihangFragment = new paihang();
        fragmentTransaction.replace(R.id.dianying_list, paihangFragment);
        fragmentTransaction.commit();
    }

    /**
     * 打开个人中心Fragment
     */
    private void openXinxiFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        Fragment xinxiFragment = new gerenxinxi();
        fragmentTransaction.replace(R.id.dianying_list, xinxiFragment);
        fragmentTransaction.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 移除所有Handler回调，防止内存泄漏
        handler.removeCallbacksAndMessages(null);
    }
}